<template>
  <div class="main-layout">
    <el-container>
      <el-header class="header">
        <div class="header-left">
          <h2>智能OJ系统</h2>
          <el-tag v-if="authStore.getIsTestMode()" type="warning" size="small" style="margin-left: 10px;">
            测试模式
          </el-tag>
        </div>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="user-info">
              {{ authStore.user?.username }}
              <el-icon><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人资料</el-dropdown-item>
                <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-container>
        <el-aside class="sidebar" :class="{ 'sidebar-collapsed': isSidebarCollapsed }" :width="sidebarWidth">
          <SideNavigation @toggle-collapse="handleSidebarToggle" />
        </el-aside>
        
        <el-main class="main-content" :class="{ 'sidebar-collapsed': isSidebarCollapsed }">
          <slot />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import SideNavigation from '@/components/SideNavigation.vue'
import { 
  ArrowDown
} from '@element-plus/icons-vue'

const router = useRouter()
const authStore = useAuthStore()

// 侧边栏折叠状态
const isSidebarCollapsed = ref(false)

// 侧边栏宽度
const sidebarWidth = computed(() => {
  return isSidebarCollapsed.value ? '64px' : '200px'
})

const handleCommand = (command: string) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'logout':
      authStore.logout()
      router.push('/login')
      break
  }
}

// 处理侧边栏折叠状态变化
const handleSidebarToggle = (collapsed: boolean) => {
  isSidebarCollapsed.value = collapsed
}
</script>

<style scoped>
.main-layout {
  height: 100vh;
}

.header {
  background: #fff;
  border-bottom: 1px solid #e4e7ed;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
}

.header-left {
  display: flex;
  align-items: center;
}

.header-left h2 {
  color: #409eff;
  margin: 0;
}

.user-info {
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
  color: #333;
}



.sidebar {
  background-color: #304156;
  height: calc(100vh - 60px);
  overflow-y: auto;
  position: fixed;
  left: 0;
  top: 60px;
  z-index: 1000;
  transition: width 0.3s ease;
}

.sidebar-collapsed {
  width: 64px !important;
}

.main-content {
  padding: 20px;
  background: #f5f5f5;
  min-height: calc(100vh - 60px);
  overflow-y: auto;
  margin-left: 200px;
  transition: margin-left 0.3s ease;
}

.main-content.sidebar-collapsed {
  margin-left: 64px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    width: 180px !important;
  }
}
</style>
